<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/6/2
 * Time: 下午9:34
 */

namespace App\Http\Controllers\xx;


use App\Http\Controllers\BaseController;
use Faker\Provider\Image;
use Illuminate\Http\Request;
use Storage;

class UploadController extends BaseController
{
    public function __construct()
    {

    }

    /**
     * 更新用户头像.
     *
     * @param  Request $request
     * @return Response
     * @translator laravelacademy.org
     */
    public function update(Request $request)
    {
        $path = $request->file('avatar')->store('avatars');
        $temp_path = tempnam(sys_get_temp_dir(), $path);
        return self::success(['url'=>asset($temp_path)],200,'ok');
    }

    // 文件上传方法
    public function upload(Request $request)
    {
        $this->p(asset('/avatars/0I61HWxf3WFZkBeV1glc3Gco6hxgsekfjk7fqbnO.jpeg'));
        if ($request->isMethod('post')) {

            $file = $request->file('picture');

            // 文件是否上传成功
            if ($file->isValid()) {

                // 获取文件相关信息
                $originalName = $file->getClientOriginalName(); // 文件原名
                $ext = $file->getClientOriginalExtension();     // 扩展名
                $realPath = $file->getRealPath();   //临时文件的绝对路径
                $type = $file->getClientMimeType();     // image/jpeg

                // 上传文件
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                // 使用我们新建的uploads本地存储空间（目录）
                $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
               $this->p($realPath);

            }

        }

        return view('upload');
    }


}